package com.example.gateway.filter;

import cn.hutool.core.util.StrUtil;
import com.example.common.base.exception.ValidateCodeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.core.Ordered;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author lengleng
 * @date 2018/7/4
 * 验证码处理
 */
@Slf4j
@Component
public class ValidateCodeGatewayFilter implements GatewayFilter, Ordered {

    public static final String DEFAULT_CODE_KEY = "DEFAULT_CODE_KEY";
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        String checkType = "";
        //校验验证码合法性
        try {
            checkCode(request);
        } catch (ValidateCodeException e) {
            ServerHttpResponse response = exchange.getResponse();
            response.setStatusCode(HttpStatus.PRECONDITION_REQUIRED);
            return response.setComplete();
        }
        return chain.filter(exchange);
    }

    /**
     * 检查code
     *
     * @param request
     * @throws ValidateCodeException 校验异常
     */
    private void checkCode(ServerHttpRequest request) throws ValidateCodeException {
        String code = request.getQueryParams().getFirst("code");
//		if (!enableValidateCode) {
//			return;
//		}
        if (StrUtil.isBlank(code)) {
            throw new ValidateCodeException();
        }

        String randomStr = request.getQueryParams().getFirst("randomStr");
        if (StrUtil.isBlank(randomStr)) {
            throw new ValidateCodeException();
        }

        String key = DEFAULT_CODE_KEY + randomStr;
        if (!redisTemplate.hasKey(key)) {
            throw new ValidateCodeException();
        }

        Object codeObj = redisTemplate.opsForValue().get(key);

        if (codeObj == null) {
            throw new ValidateCodeException();
        }

        String saveCode = codeObj.toString();
        if (StrUtil.isBlank(saveCode)) {
            redisTemplate.delete(key);
            throw new ValidateCodeException();
        }

        if (!StrUtil.equals(saveCode, code)) {
            redisTemplate.delete(key);
            throw new ValidateCodeException();
        }

        redisTemplate.delete(key);
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
